package org.lanqiao.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.jasper.tagplugins.jstl.core.ForEach;
import org.lanqiao.entity.Cost;
import org.lanqiao.entity.User;
import org.lanqiao.service.FeeService;
import org.lanqiao.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/fee")
public class FeeController {
	//分页展示
	@Resource
	private FeeService feeService;
	@RequestMapping("/feeList")
	public String showfeeList(HttpServletRequest request) { 
 		int totalRecord = feeService.totalRecord();
		int pageSize  =  5;
		int currentPage = -1;
		String curPage = request.getParameter("currentPage");
		if(curPage==null)
			currentPage=1;
		else
			currentPage=Integer.parseInt(curPage);  
		/*String pages = request.getParameter("pages");
		if(pages == null)
			pageSize = 10;
		else
			pageSize = Integer.parseInt(pages);*/
		int pageCount = (totalRecord%pageSize==0)?(totalRecord/pageSize):(totalRecord/pageSize+1);
        int a = (currentPage-1)*pageSize;
		
        request.setAttribute("totalRecord", totalRecord);
        request.setAttribute("pageSize", pageSize);
        request.setAttribute("currentPage", currentPage);
        request.setAttribute("pageCount", pageCount);
        request.setAttribute("a", a);
		
		List<Cost> cost = feeService.list(a);
		request.setAttribute("cost", cost);

		return "fee/fee_list";
	}
	/*@RequestMapping("/login")
	public String login(User user,HttpServletRequest request){
		User resultUser=userService.login(user);
		if(resultUser==null){
			request.setAttribute("user", user);//回显数据
			request.setAttribute("errorMsg", "用户名或密码错误");
			return "login";
		}else{
			HttpSession session=request.getSession();
			session.setAttribute("currentUser", resultUser);
			return "success";
		}
	}*/
	//添加数据页面的显示页面
	@RequestMapping("/fee_add")
	public String showfeeAdd(HttpServletRequest request) { 
		return "fee/fee_add";
	}
	//从前台表单提交得到的数据
	@RequestMapping("/fee_add2")
	public String showfeeAdd2(HttpServletRequest request,Cost cost) { 
		Boolean b = feeService.insert(cost);
		if(b==true) {
			request.setAttribute("flag1", 1);	
		}
		else {
			request.setAttribute("cost", cost);//回显数据
			request.setAttribute("errorMsg", "新增失败");
		}
		return "fee/fee_add";
	}
	
	//删除数据处理，接收前台数据
	@RequestMapping("/fee_delete")
	public String deletefee(HttpServletRequest request,Integer cid) { 
	    Boolean b = feeService.delete(cid);
		if(b) {
				request.setAttribute("flag1", 1);
				return "redirect:feeList.do";
		}
		else {
				request.setAttribute("errorMsg", "删除失败");
				return "redirect:feeList.do";
		}	
	}
	
	//细节展示的页面,通过id查询数据
	@RequestMapping("/fee_detail")
	public String showfeedetail(HttpServletRequest request,Integer cid) { 
		List<Cost> cost = feeService.listById(cid);
		/*for (Cost cost2 : cost) {
			System.out.println(cost2);
		}*/
		request.setAttribute("costDetail", cost);
		return "fee/fee_detail";
	}
	//数据修改显示的页面
		@RequestMapping("/fee_modi")
		public String showfeemodi(HttpServletRequest request,Integer cid) {
			List<Cost> cost1 = feeService.listById(cid);
			request.setAttribute("costModi", cost1);
			return "fee/fee_modi";
		}
	//数据修改的页面
	@RequestMapping("/fee_modi2")
	public String showfeemodi2(HttpServletRequest request,Cost cost) { 
		/*List<Cost> cost1 = feeService.listById(cost.getCid());
		request.setAttribute("costModi1", cost1);*/
		Boolean b = feeService.update(cost);
		
		if(b) {
			 //request.setAttribute("costModi", cost);
		    return "forward:fee_modi.do?cid="+cost.getCid();
		   
		}
		else {
			return "forward:fee_modi.do";
		}
	}
	
	/*@RequestMapping("/add")
	public String addStudent(Student student , Model model){
		System.out.println(student);
		model.addAttribute("student", student);
		return "ck2";	
	}*/
}

